package com.beaut.project.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beaut.project.dao.StatisticMapper;
import com.beaut.project.util.StaticData;
import com.beaut.project.util.Utility;

@Transactional(rollbackFor = Exception.class)
@Service("statisticService")
public class StatisticService {

	@Autowired
	private StatisticMapper statisticMapper;
	
	public String gdStatistic(Map<String,Object> map){
		Map<String,Object> rtMap = new HashMap<String,Object>();
		//期初统计数
		int num1 = statisticMapper.getGDNum1(map);
		rtMap.put("num1", num1);
		//本期新增数
		int num2 = statisticMapper.getGDNum2(map);
		rtMap.put("num2", num2);
		//本期关闭数
		int num3 = statisticMapper.getGDNum3(map);
		rtMap.put("num3", num3);
		//未关闭数量合计
		int num4 = statisticMapper.getGDNum4(map);
		rtMap.put("num4", num4);
		//已关闭数量
		int num5 = statisticMapper.getGDNum5(map);
		rtMap.put("num5", num5);
		//工单总计
		int num6 = statisticMapper.getGDNum6(map);
		rtMap.put("num6", num6 );
		
		try {
			String end_date = String.valueOf(map.get("end_date"));
			String start_date = String.valueOf(map.get("start_date"));
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(end_date);
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			//开工三个月内工单数
			calendar.add(Calendar.MONTH, -3);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num7 = statisticMapper.getGDNum7(map);
			rtMap.put("date1", map.get("start_date"));
			rtMap.put("date2", map.get("end_date"));
			rtMap.put("num7", num7);
			//开工三至六个月工单数
			map.put("end_date",map.get("start_date"));
			calendar.add(Calendar.MONTH, -3);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num8 = statisticMapper.getGDNum7(map);
			rtMap.put("date3", map.get("start_date"));
			rtMap.put("date4", map.get("end_date"));
			rtMap.put("num8", num8);
			//开工六至十二个月工单数
			map.put("end_date",map.get("start_date"));
			calendar.add(Calendar.MONTH, -6);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num9 = statisticMapper.getGDNum7(map);
			rtMap.put("date5", map.get("start_date"));
			rtMap.put("date6", map.get("end_date"));
			rtMap.put("num9", num9);
			//开工一至两年工单数
			map.put("end_date",map.get("start_date"));
			calendar.add(Calendar.YEAR, -1);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num10 = statisticMapper.getGDNum7(map);
			rtMap.put("date7", map.get("start_date"));
			rtMap.put("date8", map.get("end_date"));
			rtMap.put("num10", num10);
			//开工二年以上工单数
			map.put("end_date",map.get("start_date"));
			map.put("start_date","");
			int num11 = statisticMapper.getGDNum7(map);
			rtMap.put("date9", map.get("start_date"));
			rtMap.put("date10", map.get("end_date"));
			rtMap.put("num11", num11);
			//已立项工单数
			map.put("start_date", start_date);
			map.put("end_date", end_date);
			int num12 = statisticMapper.getGDNum12(map);
			rtMap.put("num12", num12);
			//未立项工单数
			int num13 = statisticMapper.getGDNum13(map);
			rtMap.put("num13", num13);
			//超期未立项工单数
			calendar.setTime(date);
			calendar.add(Calendar.YEAR, -1);//超期时间定义为：开工一年以上未归属项目的
			map.put("end_date",dateFormat.format(calendar.getTime()));
			int num14 = statisticMapper.getGDNum13(map);
			rtMap.put("num14", num14);
			return Utility.toJson(rtMap);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String xmStatistic(Map<String,Object> map){
		Map<String,Object> rtMap = new HashMap<String,Object>();
		//期初统计数
		int num1 = statisticMapper.getXMNum1(map);
		rtMap.put("num1", num1);
		//本期新增数
		int num2 = statisticMapper.getXMNum2(map);
		rtMap.put("num2", num2);
		//本期关闭数
		int num3 = statisticMapper.getXMNum3(map);
		rtMap.put("num3", num3);
		//未关闭数量合计
		int num4 = statisticMapper.getXMNum4(map);
		rtMap.put("num4", num4);
		//已关闭数量
		int num5 = statisticMapper.getXMNum5(map);
		rtMap.put("num5", num5);
		//项目总计
		int num6 = statisticMapper.getXMNum6(map);
		rtMap.put("num6", num6);
		
		try {
			String start_date = String.valueOf(map.get("start_date"));
			String end_date = String.valueOf(map.get("end_date"));
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(end_date);
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			//开工三个月内项目数
			calendar.add(Calendar.MONTH, -3);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num7 = statisticMapper.getXMNum7(map);
			rtMap.put("date1", map.get("start_date"));
			rtMap.put("date2", map.get("end_date"));
			rtMap.put("num7", num7);
			//开工三至六个月项目数
			map.put("end_date",map.get("start_date"));
			calendar.add(Calendar.MONTH, -3);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num8 = statisticMapper.getXMNum7(map);
			rtMap.put("date3", map.get("start_date"));
			rtMap.put("date4", map.get("end_date"));
			rtMap.put("num8", num8);
			//开工六至十二个月项目数
			map.put("end_date",map.get("start_date"));
			calendar.add(Calendar.MONTH, -6);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num9 = statisticMapper.getXMNum7(map);
			rtMap.put("date5", map.get("start_date"));
			rtMap.put("date6", map.get("end_date"));
			rtMap.put("num9", num9);
			//开工一至两年项目数
			map.put("end_date",map.get("start_date"));
			calendar.add(Calendar.YEAR, -1);
			map.put("start_date",dateFormat.format(calendar.getTime()));
			int num10 = statisticMapper.getXMNum7(map);
			rtMap.put("date7", map.get("start_date"));
			rtMap.put("date8", map.get("end_date"));
			rtMap.put("num10", num10);
			//开工二年以上项目数
			map.put("end_date",map.get("start_date"));
			map.put("start_date","");
			int num11 = statisticMapper.getXMNum7(map);
			rtMap.put("date9", map.get("start_date"));
			rtMap.put("date10", map.get("end_date"));
			rtMap.put("num11", num11);
			//未审计项目数
			map.put("start_date",start_date);
			map.put("end_date",end_date);
			int num12 = statisticMapper.getXMNum12(map);
			rtMap.put("num12", num12);
			//已审计未开票项目数
			int num13 = statisticMapper.getXMNum13(map);
			rtMap.put("num13", num13);
			//已开票未回款项目数
			int num14 = statisticMapper.getXMNum14(map);
			rtMap.put("num14", num14);
			//已回款项目数
			int num15 = statisticMapper.getXMNum15(map);
			rtMap.put("num15", num15);
			//项目数合计
			rtMap.put("num16", num12+num13+num14+num15);
			//超期未审计数
			calendar.setTime(date);
			calendar.add(Calendar.YEAR, -1);//超期时间定义为：立项时间超过一年
			map.put("end_date",dateFormat.format(calendar.getTime()));
			map.put("start_date","");
			int num17 = statisticMapper.getXMNum17(map);
			rtMap.put("num17", num17);
			//超期未回款数
			int num18 = statisticMapper.getXMNum18(map);
			rtMap.put("num18", num18);
			//超期未回款金额
			rtMap.put("num19", statisticMapper.getXMNum19(map));
			//进度开票项目数
			map.put("start_date",start_date);
			map.put("end_date",end_date);
			int num20 = statisticMapper.getXMNum20(map);
			rtMap.put("num20", num20);
			//进度开票总额
			rtMap.put("num21", statisticMapper.getXMNum21(map));
			//进度开票回款总额
			rtMap.put("num22", statisticMapper.getXMNum22(map));
			return Utility.toJson(rtMap);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String listPageGDBase(Map<String,Object> map){
		try {
			String end_date = String.valueOf(map.get("end_date"));
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(end_date);
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			String type = String.valueOf(map.get("type"));
			List<Map<String,Object>> list = null;
			if("1".equals(type)){
				//开工三个月内工单数
				calendar.add(Calendar.MONTH, -3);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				list = statisticMapper.listPageGDBase(map);
			}else if("2".equals(type)){
				//开工三至六个月工单数
				calendar.add(Calendar.MONTH, -3);
				map.put("end_date",dateFormat.format(calendar.getTime()));
				calendar.add(Calendar.MONTH, -3);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				map.put("type", "1");
				list = statisticMapper.listPageGDBase(map);
			}else if("3".equals(type)){
				//开工六至十二个月工单数
				calendar.add(Calendar.MONTH, -6);
				map.put("end_date",dateFormat.format(calendar.getTime()));
				calendar.add(Calendar.MONTH, -6);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				map.put("type", "1");
				list = statisticMapper.listPageGDBase(map);
			}else if("4".equals(type)){
				//开工一至两年工单数
				calendar.add(Calendar.YEAR, -1);
				map.put("end_date",dateFormat.format(calendar.getTime()));
				calendar.add(Calendar.YEAR, -1);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				map.put("type", "1");
				list = statisticMapper.listPageGDBase(map);
			}else if("5".equals(type)){
				//开工二年以上工单数
				calendar.add(Calendar.YEAR, -2);
				map.put("end_date",dateFormat.format(calendar.getTime()));
				map.put("start_date","");
				map.put("type", "1");
				list = statisticMapper.listPageGDBase(map);
			}else if("17".equals(type)){
				//超期未立项工单数
				calendar.setTime(date);
				calendar.add(Calendar.YEAR, -1);//超期时间定义为：立项时间超过一年
				map.put("end_date",dateFormat.format(calendar.getTime()));
				map.put("start_date","");
				map.put("type", "15");
				list = statisticMapper.listPageGDBase(map);
			}else{
				list = statisticMapper.listPageGDBase(map);
			}
			if(list != null && list.size() > 0){
				for(int i=0;i<list.size();i++){
					list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
				}
				StringBuffer sb = new StringBuffer("");
				sb.append(Utility.lstMaptoJson(list));
				Utility.putSuccess(map);
				return sb.toString();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String listPageXMBase(Map<String,Object> map){
		try {
			String end_date = String.valueOf(map.get("end_date"));
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(end_date);
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			String type = String.valueOf(map.get("type"));
			List<Map<String,Object>> list = null;
			if("7".equals(type)){
				//开工三个月内项目数
				calendar.add(Calendar.MONTH, -3);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				list = statisticMapper.listPageXMBase(map);
			}
			else if("8".equals(type)){
				//开工三至六个月项目数
				map.put("end_date",map.get("start_date"));
				calendar.add(Calendar.MONTH, -3);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				list = statisticMapper.listPageXMBase(map);
			}
			else if("9".equals(type)){
				//开工六至十二个月项目数
				map.put("end_date",map.get("start_date"));
				calendar.add(Calendar.MONTH, -6);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				list = statisticMapper.listPageXMBase(map);
			}
			else if("10".equals(type)){
				//开工一至两年项目数
				map.put("end_date",map.get("start_date"));
				calendar.add(Calendar.YEAR, -1);
				map.put("start_date",dateFormat.format(calendar.getTime()));
				list = statisticMapper.listPageXMBase(map);
			}
			else if("11".equals(type)){
				//开工二年以上项目数
				map.put("end_date",map.get("start_date"));
				map.put("start_date","");
				list = statisticMapper.listPageXMBase(map);
			}
			else if("17".equals(type)){
				//超期未审计数
				calendar.setTime(date);
				calendar.add(Calendar.YEAR, -1);//超期时间定义为：立项时间超过一年
				map.put("end_date",dateFormat.format(calendar.getTime()));
				map.put("start_date","");
				list = statisticMapper.listPageXMBase(map);
			}else{
				list = statisticMapper.listPageXMBase(map);
			}
			StringBuffer sb = new StringBuffer("");
			sb.append(Utility.lstMaptoJson(list));
			Utility.putSuccess(map);
			return sb.toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String listPageHistory(Map<String,Object> map){
		List<Map<String,Object>> list= statisticMapper.listPageHistory(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	
	public String listPageGdLoan(Map<String,Object> map){
		List<Map<String,Object>> list= statisticMapper.listPageGdLoan(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGdStatus(Map<String,Object> map){
		List<Map<String,Object>> list= statisticMapper.listPageGdStatus(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXmStatus(Map<String,Object> map){
		List<Map<String,Object>> list= statisticMapper.listPageXmStatus(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageGd(Map<String,Object> map){
		List<Map<String,Object>> list= statisticMapper.listPageGd(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
}
